from database.mysqlhelper import MySqLHelper
from utils import dict_util

db = MySqLHelper()

insert_sql = 'insert into t_monitor_strategy_config (f_code, f_user_code, f_strategy_code, f_valid_beg_date, f_valid_end_date, f_bs_flag, f_conf_info,f_type, f_status, f_trigger_time) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) '
select_sql = 'select f_id, f_code, f_user_code, f_strategy_code, f_valid_beg_date, f_valid_end_date, f_bs_flag, f_conf_info, f_type, f_status, f_trigger_time, f_update_time from t_monitor_strategy_config '
update_sql = 'update t_monitor_strategy_config '


class StrategyCons:
    id = 'id'
    code = 'code'
    user_code = 'user_code'
    strategy_code = 'strategy_code'
    valid_beg_date = 'valid_beg_date'
    valid_end_date = 'valid_end_date'
    bs_flag = 'bs_flag'
    conf_info = 'conf_info'
    type = 'type'
    status = 'status'
    up_time = 'up_time'
    trigger_time = 'trigger_time'

    validdays = 'validdays'
    stockprice = 'stockprice'
    avedays = 'avedays'
    iodays = 'iodays'
    macddays = 'macddays'
    pricetype = 'pricetype'
    avetype = 'avetype'
    iotype = 'iotype'
    macdtype = 'macdtype'
    promptinfo = 'promptinfo'
    op_list_bs_conf = [{'key': '买入', 'value': '1'}, {'key': '卖出', 'value': '2'}]
    op_list_price_conf = [{'key': '低于', 'value': '1'}, {'key': '高于', 'value': '2'}]
    op_list_ave_conf = [{'key': '冲破', 'value': '1'}, {'key': '跌破', 'value': '2'},
                        {'key': '稳高', 'value': '3'}, {'key': '稳低', 'value': '4'}]
    op_list_macd_conf = [{'key': '拐高', 'value': '1'}, {'key': '拐底', 'value': '2'},
                         {'key': '升高', 'value': '3'}, {'key': '降低', 'value': '4'}]
    op_list_io_conf = [{'key': '转正', 'value': '1'}, {'key': '转负', 'value': '2'}]


def tuple_to_data(res):
    return {'id': res[0],
            'code': str(res[1], encoding="utf-8"),
            'user_code': str(res[2], encoding="utf-8"),
            'strategy_code': str(res[3], encoding="utf-8"),
            'valid_beg_date': res[4],
            'valid_end_date': res[5],
            'bs_flag': str(res[6], encoding="utf-8"),
            'conf_info': eval(str(res[7], encoding="utf-8")),
            'type': res[8],
            'status': res[9],
            'trigger_time': res[10],
            'up_time': res[11]}


def data_to_tuple(data):
    return (data['code'], data['user_code'], data['strategy_code'], data['valid_beg_date'],
            data['valid_end_date'], data['bs_flag'], data['conf_info'], data['type'], data['status'],
            dict_util.get_default(data, 'trigger_time', ''))


# 插入一条
def insert_one(data):
    if not data:
        return 0
    return db.insertone(insert_sql, data_to_tuple(data))


# 更新状态
def update_status_by_id(id, status):
    sql = update_sql + ' set f_status = %s where f_id = %s '
    return db.update(sql, (status, id))


# 更新状态
def update_trigger_by_id(id, trigger_time):
    sql = update_sql + ' set f_trigger_time = %s where f_id = %s '
    return db.update(sql, (trigger_time, id))


# 根据股票code获取
def select_by_stock(stock_code):
    sql = select_sql + ' where f_code = %s and f_status = 1 and f_valid_end_date > NOW() order by f_type'
    return format_res(db.selectall(sql, (stock_code)))


# 根据用户和股票code获取
def select_by_user_stock(user_code, stock_code):
    sql = select_sql + ' where f_user_code = %s and f_code = %s and f_status = 1 order by f_type'
    return format_res(db.selectall(sql, (user_code, stock_code)))


# 根据用户和股票code策略获取
def select_by_user_stock_strategy_code(user_code, stock_code, strategy_code):
    sql = select_sql + ' where f_user_code = %s and f_code = %s and f_strategy_code = %s limit 1'
    return format_one(db.selectone(sql, (user_code, stock_code, strategy_code)))


# 格式化结果
def format_res(res):
    list = []
    for p in res:
        list.append(format_one(p))
    return list


# 格式化结果
def format_one(res):
    if not res:
        return None
    return tuple_to_data(res)
